﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DriveBabyDrive.Models;
using System.Data;
using DataLayer;
using System.Web.Security;
using System.Collections;
using System.Web.Script.Serialization;
using DriveBabyDrive.Util;

namespace DriveBabyDrive.Controllers
{
    [Authorize]
    public class ReadingController : Controller
    {
        //
        // GET: /Reading/

        public ActionResult Index()
        {
            return RedirectToAction("NewReading");
        }
        [HttpGet]
        public ActionResult NewReading()
        {
            ViewData["UserVehicles"] = getUserVehicleLists();
            return View();
        }
        [HttpPost]
        public ActionResult NewReading(ReadingModel model)
        {           
            //save to the database
            //redirect to the last 3 readings view
            ReadingModel reading = this.saveReadingToDatabase(model);            
            return View("ReadingResult",reading);
        }
        public ActionResult MyReadings()
        {
            ViewData["UserVehicles"] = getUserVehicleLists();
            return View();
        }

        public ActionResult MyLocations()
        {
            string browserAgent = Request.UserAgent.ToString().ToLower();
            if (!(browserAgent.Contains("android") || browserAgent.Contains("iphone")))
            {
                return RedirectToAction("MyLocationsDesktop");
            } 
            return View();
        }

        

        public ActionResult MyLocationsDesktop()
        {
            return View();
        }
        /// <summary>
        /// Gets readings for a specified vehicle of the user
        /// </summary>
        /// <param name="userVehicleID">vehicle to get readings for</param>
        /// <returns></returns>
        [HttpGet]
        public string GetUserReadingsByVehicleID(int userVehicleID)
        {
            DataSet dsReadings = (new ReadingDL()).getReadingsForUserByVehicle(userVehicleID);
            string jsonReadings = Util.CommonFunctions.GetJson(dsReadings.Tables[0]);
            //string jsonReadings = new JavaScriptSerializer().Serialize(dsReadings.GetXml()).ToString();
            return jsonReadings;
        }
        /// <summary>
        /// Gets all User readings
        /// </summary>
        /// <returns>json table for all readings</returns>
        [HttpGet]
        public string GetUserReadings()
        {
            DataSet dsReadings = (new ReadingDL()).getReadingsForUser(Membership.GetUser().UserName);
            string jsonReadings = Util.CommonFunctions.GetJson(dsReadings.Tables[0]);
            //string jsonReadings = new JavaScriptSerializer().Serialize(dsReadings.GetXml()).ToString();
            return jsonReadings;
        }
        private ReadingModel getReadingModel(DataRow readingRow)
        {
            ReadingModel reading = new ReadingModel();
            reading.Odometer = decimal.Parse(readingRow["Odometer"].ToString());
            reading.FuelAdded = decimal.Parse(readingRow["FuelAdded"].ToString());
            reading.FuelPrice = decimal.Parse(readingRow["FuelPrice"].ToString());
            reading.UserVehicleID = int.Parse(readingRow["UserVehicleID"].ToString());
            reading.Lattitude = decimal.Parse(readingRow["Lattitude"].ToString());
            reading.Longitude = decimal.Parse(readingRow["Longitude"].ToString());
            reading.FuelEfficiency = decimal.Parse(readingRow["FuelEfficiency"].ToString());
            reading.CostPerFuelUnit = decimal.Parse(readingRow["CostPerFuelUnit"].ToString());
            return reading;
        }
        private ReadingModel saveReadingToDatabase(ReadingModel reading)
        {
            DataSet readingDS = (new ReadingDL()).addReading(reading.UserVehicleID, reading.Odometer, reading.FuelAdded, reading.FuelPrice, reading.Lattitude, reading.Longitude);
            return this.getReadingModel(readingDS.Tables[0].Rows[0]);
        }

        /// <summary>
        ///  generate a list of uservehicles
        /// </summary>
        /// <returns></returns>
        private SelectList getUserVehicleLists()
        {
            DataTable dtVehicles = (new VehiclesDL()).getUserVehicles(Membership.GetUser().UserName).Tables[0];
            ArrayList vehicles = new ArrayList();
            foreach(DataRow vehicle in dtVehicles.Rows){
                vehicles.Add(new { UserVehicleID = vehicle["UserVehicleID"].ToString(), Name = vehicle["Name"].ToString()});
            }

            return new SelectList(vehicles.ToArray(), "UserVehicleID", "Name");

        }
        

    }
}
